package org.keyyond.ms.core.util;

import org.springframework.stereotype.Component;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Component
public class CommonBeanUtil {

	/**
	 * 对返回结果进行组装
	 * 
	 * @author YCR @param status true/false true代表成功，false代表失败 @param message
	 *         JSONObject对象，具体格式参考回执格式说明文档 @return 转换后的json对象 @throws
	 */
	public static String returnResult1(boolean status, JSONObject message) {
		JSONObject result = new JSONObject();
		try {
			if (status) {
				result.put(ConstantResult.RESULT_RESULT, true);
				result.put(ConstantResult.RESULT_DATA, message);
			} else {
				result.put(ConstantResult.RESULT_RESULT, false);
				result.put(ConstantResult.RESULT_ERRMSG, message.getString(ConstantResult.RESULT_ERRMSG));
				result.put(ConstantResult.RESULT_ERRCODE, message.getString(ConstantResult.RESULT_ERRCODE));
			}
		} catch (Exception e) {
			result.put(ConstantResult.RESULT_RESULT, false);
			result.put(ConstantResult.RESULT_ERRMSG, ConstantResult.RESULT_ERRMSG_FORMAT_JSON);
			result.put(ConstantResult.RESULT_ERRCODE, ConstantResult.RESULT_ERRCODE_FORMAT_JSON);
			e.printStackTrace();
		}
		return result.toString();
	}

	public static String returnResult(boolean status, JSONArray dataArr, String message) {
		JSONObject result = new JSONObject();
		try {
			result.put(Constant.DATA, dataArr);
			JSONObject metaJSon = new JSONObject();
			metaJSon.put(Constant.SUCCESS, status);
			metaJSon.put(Constant.MSG, message);
			result.put(Constant.META, metaJSon);

		} catch (Exception e) {
			e.printStackTrace();
			result.put(Constant.DATA, null);
			JSONObject metaJSon = new JSONObject();
			metaJSon.put(Constant.SUCCESS, false);
			metaJSon.put(Constant.MSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
			result.put(Constant.META, metaJSon);
		}
		return result.toString();
	}
	
	public static String returnResult(boolean status, JSONArray dataArr) {
		JSONObject result = new JSONObject();
		try {
			result.put(Constant.DATA, dataArr);
			JSONObject metaJSon = new JSONObject();
			metaJSon.put(Constant.SUCCESS, status);
			metaJSon.put(Constant.MSG, ConstantResult.RESULT_SUCCESS_ACTION);
			result.put(Constant.META, metaJSon);

		} catch (Exception e) {
			e.printStackTrace();
			result.put(Constant.DATA, null);
			JSONObject metaJSon = new JSONObject();
			metaJSon.put(Constant.SUCCESS, false);
			metaJSon.put(Constant.MSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
			result.put(Constant.META, metaJSon);
		}
		return result.toString();
	}
	
	public static String returnResult(boolean status, String message) {
		JSONObject result = new JSONObject();
		try {
			result.put(Constant.DATA, null);
			JSONObject metaJSon = new JSONObject();
			metaJSon.put(Constant.SUCCESS, status);
			metaJSon.put(Constant.MSG, message);
			result.put(Constant.META, metaJSon);

		} catch (Exception e) {
			e.printStackTrace();
			result.put(Constant.DATA, null);
			JSONObject metaJSon = new JSONObject();
			metaJSon.put(Constant.SUCCESS, false);
			metaJSon.put(Constant.MSG, ConstantResult.RESULT_ERRMSG_EXCEPTION);
			result.put(Constant.META, metaJSon);
		}
		return result.toString();
	}

}
